package com.cm386.final_project_timezonealert;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cm386.final_project_timezonealert.model.AlertTimeService;
import com.cm386.final_project_timezonealert.model.EventTimePickerDialogFragment;
import com.cm386.final_project_timezonealert.model.TimeZoneService;

public class EventFieldActivity extends Activity {
	public static final String EVENT_CITY_NAME_KEY	   = "EVENT_CITY_NAME";
	public static final String EVENT_CITY_TIMEZONE_ID_KEY = "EVENT_CITY_TIMEZONE_ID";
	private int eventHour;
	private int eventMinute;
	private String eventCityName;
	private String eventCityTimeZoneId;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event_field);
		
		Bundle data 			  = this.getIntent().getExtras();
		eventCityName			  = data.getString(EVENT_CITY_NAME_KEY);
		eventCityTimeZoneId 	  = data.getString(EVENT_CITY_TIMEZONE_ID_KEY);
		eventHour		  		  = data.getInt("HOUR");
		eventMinute	  			  = data.getInt("MINUTE");
		TextView cityNameTextView = (TextView)this.findViewById(R.id.event_field_set_city_name_text);
		cityNameTextView.setText(eventCityName);
		
		EditText editText		  = (EditText)this.findViewById(R.id.event_field_set_time_text);
		String eventHourString    = String.valueOf(eventHour);
		String eventMinuteString  = String.valueOf(eventMinute);
		if (eventHourString.length() < 2)
			eventHourString = "0" + eventHourString;
		if (eventMinuteString.length() < 2)
			eventMinuteString = "0" + eventMinuteString;
		editText.setText(eventHourString + ":" + eventMinuteString);
		
		Button modifyTimeButton   = (Button)this.findViewById(R.id.event_field_set_time_button);
		modifyTimeButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				FragmentManager fm 		= getFragmentManager();
				EventTimePickerDialogFragment eventTimePickerDialogFragment = EventTimePickerDialogFragment.getInstance(eventHour, eventMinute);
				eventTimePickerDialogFragment.show(fm, "Event Time Picker");
			}
		});
		
		Button cancelButton = (Button) this.findViewById(R.id.event_field_cancel_button);
		cancelButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent mainActivity = new Intent(EventFieldActivity.this, MainActivity.class);
				mainActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mainActivity);
			}
		});
		
		Button confirmButton = (Button)this.findViewById(R.id.event_field_set_event_button);
		confirmButton.setOnClickListener(new OnClickListener() {
			EditText eventNameView;
			public OnClickListener init(EditText eventNameView){
				this.eventNameView = eventNameView;
				return this;
			}
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String convertHour = TimeZoneService.toDestinationHourStringByReference(eventHour, eventMinute, TimeZone.getTimeZone(eventCityTimeZoneId), TimeZone.getDefault());
				String convertMinute = TimeZoneService.toDestinationMinutesStringByReference(eventHour, eventMinute, TimeZone.getTimeZone(eventCityTimeZoneId), TimeZone.getDefault());
				Date date = new Date();
				SimpleDateFormat formatYear  = new SimpleDateFormat("yyyy");
				SimpleDateFormat formatMonth = new SimpleDateFormat("MM");
				SimpleDateFormat formatDay   = new SimpleDateFormat("dd");
				SimpleDateFormat formatFull	 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Calendar calendar = Calendar.getInstance();
				calendar.set(Calendar.YEAR, Integer.parseInt(formatYear.format(date)));
				calendar.set(Calendar.MONTH, Integer.parseInt(formatMonth.format(date)) - 1);
				calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(formatDay.format(date)));
				calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(convertHour));
				calendar.set(Calendar.MINUTE, Integer.parseInt(convertMinute));
				
				Calendar calendarCurrent = Calendar.getInstance();
				calendarCurrent.setTime(date);
				if (calendar.compareTo(calendarCurrent) > 0){
				} else {
					calendar.add(Calendar.DAY_OF_MONTH, 1);
				}
				setAlert(formatFull.format(calendar.getTime()));
				Toast.makeText(EventFieldActivity.this, "Set Alert at " + formatFull.format(calendar.getTime()), Toast.LENGTH_LONG).show();
				Intent mainActivity = new Intent(EventFieldActivity.this, MainActivity.class);
				mainActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(mainActivity);
			}
			
			public void setAlert(String time){
				Intent timeService = new Intent(EventFieldActivity.this, AlertTimeService.class);
				Bundle bData	   = new Bundle();
				bData.putString(AlertTimeService.TIME_KEY, time);
				bData.putString(AlertTimeService.NOIFICATION_TITLE_KEY, eventNameView.getText().toString());
				timeService.putExtras(bData);
				EventFieldActivity.this.getApplicationContext().startService(timeService);
			}
		}.init((EditText)findViewById(R.id.event_field_set_event_name_text)));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.event_field, menu);	return true;
	}
	
	public void timePickerSetTimeCallBack(int hour, int minutes){
		eventHour   = hour;
		eventMinute = minutes;
		EditText editText   = (EditText)this.findViewById(R.id.event_field_set_time_text);
		String hourString   = String.valueOf(hour);
		String minuteString = String.valueOf(minutes);
		if (hourString.length() < 2)
			hourString = "0" + hourString;
		if (minuteString.length() < 2)
			minuteString = "0" + minuteString;
		editText.setText(hourString + ":" + minuteString);
	}

}
